<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Ext.Panel</title>
    <link rel="stylesheet" type="text/css" href="../lib/ext/resources/css/ext-all-neptune.css"/>
    <script type="text/javascript" src="../lib/ext/ext-all.js"></script>
    <script type="text/javascript" src="../lib/gf/common_grid.js"></script>
</head>
<script type="text/javascript">
Ext.Loader.setConfig({enabled: true});

Ext.Loader.setPath('Ext.ux', '/ext4/ux/');
Ext.require([
    'Ext.grid.*',
    'Ext.data.*',
    'Ext.util.*',
    'Ext.toolbar.Paging',
    'Ext.ModelManager',
    'Ext.tip.QuickTipManager'
]);
var win, searchForm;
Ext.onReady(function () {
    Ext.define('WxSrcMgt', {
        extend: 'Ext.data.Model',
        fields: [
            '_id','name','type','content','init_date','title','category'
        ],
        idProperty: '_id'
    });

    // create the Data Store
    var store = Ext.create('Ext.data.Store', {
        pageSize: 10,
        model: 'WxSrcMgt',
        remoteSort: true,
        proxy: {
            type: 'ajax',
            url: '/wx/srcmgt/list',
            reader: {
                type: 'json',
                root: 'data',
                totalProperty: 'count'
            },
            simpleSortMode: true
        },
        sorters: [
            {
                property: 'init_date'
            }
        ]
    });

    searchForm = new Ext.FormPanel({
        labelWidth: 75, // label settings here cascade unless overridden
        region: 'north',
        defaultType: 'textfield',
        items: [
            {
                fieldLabel: '标题',
                name: 'title',
                allowBlank: true
            },{
                fieldLabel : '日期',
                name : 'init_date',
                xtype : 'datefield',
                editable  : false,
                format: 'Ymd'
            }
        ]
    });

    var grid = Ext.create('Ext.grid.Panel', {
        store: store,
        loadMask: true,
        region: 'center',
        selModel: Ext.create('Ext.selection.CheckboxModel'),
        columns: [
            {
                text: "素材编号",
                dataIndex: '_id'                        ,
                width : '20%'
            },
            {text: '素材标题',dataIndex: 'title',width:'20%'},
            {
                text: '素材类型',
                dataIndex: 'type',
                width:'10%',
                renderer:function(val){
                    return val == "news" ? "图文资源" : "文本";
                }
            },
            {
                text: '所属栏目',
                dataIndex: 'category',
                width:'10%'
                /*
                renderer:function(val){
                    if(val == "global_market_MK")   return "海外聚焦(" + val + ")";
                    else if(val == "hot_products_MK")  return "热销产品(" + val + ")";
                    else return "无";
                } */
            },
            {text: '日期',dataIndex: 'init_date',width:'20%'},
            {text: '素材内容',dataIndex: 'content',width:'30%'}
        ],
        // paging bar on the bottom
        bbar: Ext.create('Ext.PagingToolbar', {
            store: store,
            displayInfo: true,
            displayMsg: 'Displaying {0} - {1} of {2}',
            emptyMsg: "没有数据！！！"

        }),
        tbar: [
            {
                pressed: true,
                text: '搜  索',
                icon : '../img/search.png',
                handler: doSearch
            },
            {
                pressed: true,
                text: "修  改",
                icon : '../img/edit.png',
                handler: function(){
                    if(grid.getSelectionModel().getSelection().length >= 0){
                        var type = grid.getSelectionModel().getSelection()[0].data.type;
                        if(type === "news"){
                            doEdit(grid,"/admin/wx_srcmgt/add_news.html","修改微信素材信息")
                        }
                        else{
                            doEdit(grid,"/admin/wx_srcmgt/add_text.html","修改微信素材信息")
                        }
                    }
                    else{
                        Ext.MessageBox.show({title: "消息提示",
                            msg: "请选择一条记录",
                            width: 200,
                            icon: Ext.MessageBox.INFO
                        });
                    }

                }
            },
            {
                pressed: true,
                icon : '../img/add.png',
                text: "添加文本资源",
                handler: function(){doAdd("/admin/wx_srcmgt/add_text.html","新增微信素材信息")}
            },
            {
                pressed: true,
                icon : '../img/add.png',
                text: "添加图文资源",
                handler: function(){doAdd("/admin/wx_srcmgt/add_news.html","新增微信素材信息")}
            },
            {
                pressed: true,
                icon : '../img/add.png',
                text: "触发推送",
                handler: function(){
                    Ext.MessageBox.confirm('消息提示', '确定推送消息？', function (btn) {
                        if (btn == "yes") {
                            var orows = grid.getSelectionModel().getSelection();
                            if (orows.length == 0) {
                                Ext.MessageBox.show({title: "消息提示",
                                    msg: "请选择一条记录",
                                    width: 200,
                                    icon: Ext.MessageBox.INFO
                                });
                                return;
                            }
                            Ext.Ajax.request({
                                url: "/wx/srcmgt/trigger",
                                params: { id: orows[0].get('_id') },
                                method: 'POST',
                                success: function (response, options) {
                                    Ext.MessageBox.alert('消息提示', '推送成功！');
                                    store.loadPage(1);
                                },
                                failure: function (response, options) {
                                    Ext.MessageBox.alert('消息提示', '推送失败！');
                                }
                            });
                        }
                    })
                }
            },
            {
                pressed: true,
                icon : '../img/delete.png',
                text: "删  除",
                handler: function(){doDel(grid,"/wx/srcmgt/del",store)}
            }

        ]
    });


    var viewport = new Ext.Viewport({
                layout: 'border',
                renderTo: "topic-grid",
                items: [searchForm,grid]}
    );

    function doSearch(){
        var params = searchForm.getValues();
        store.loadPage(1,{params:params});
    }
    store.on('beforeload',function(store, operation,eOpts){
        var params = searchForm.getValues();
        operation.params= params;
    },this);
    // trigger the data store load
    store.loadPage(1);
    window.refresh = function(){
        store.loadPage(1);
    }

    window.closeWin = function(){
        win.hide();
    }
});
</script>
<body>
<div id="topic-grid" style="height: 1024px;width:100%"></div>
<p>The js is not minified so it is readable. See <a href="panel.js">panel.js</a>.</p>
</body>
</html>